<?php
/**
 * Author: FoxxxZhu
 * Date: 2016/7/11
 * Time: 18:36
 */

class StatGroupBuyCount {

    protected $mongoManager;
    protected $mongoCollection;
    protected static $_instance = [];
    protected $config;

    protected function __construct($date = '')
    {
        require_once __DIR__.DIRECTORY_SEPARATOR.'vendor'.DIRECTORY_SEPARATOR.'autoload.php';
        $config = self::getConfig();
        $this->mongoManager = new \MongoDB\Driver\Manager($config['dsn']);
        $this->mongoCollection = new \MongoDB\Collection($this->mongoManager,$config['database'],$config['tablePrefix'].$date);
    }

    public static function instance($date = '')
    {
        if(!isset(self::$_instance[$date]))
        {
            self::$_instance[$date] = new Static($date);
        }
        return self::$_instance[$date];
    }

    /**
     * 用户数
     * @param $where
     * @param string $date
     * @return int
     */
    public static function getUserNumber($where,$date = '')
    {
        $num = 0;
        try{
            $cursor = self::instance()->mongoCollection->aggregate(
                [
                    ['$match'=>$where],
                    ['$group'=>['_id'=>'$user_id','count'=>['$sum'=>1]]],
                    ['$project'=>['my'=>['$add'=>[1]]]],
                    ['$group'=>['_id'=>'$my','num'=>['$sum'=>'$my']]],
                ]);
            $document = current($cursor->toArray());
            if(!empty($document))
            {
                $num = $document->num;
            }
        }
        catch(\Exception $e)
        {
            \Yii::error($e->getMessage());
        }
        return $num;
    }

    /**
     * 粉丝数
     * @param $where
     * @param string $date
     * @return int
     */
    public static function getFansNumber($where,$date = '')
    {
        $where['is_follow'] = true;
        $num = 0;
        try{
            $cursor = self::instance()->mongoCollection->aggregate(
                [
                    ['$match'=>$where],
                    ['$group'=>['_id'=>'$user_id','count'=>['$sum'=>1]]],
                    ['$project'=>['my'=>['$add'=>[1]]]],
                    ['$group'=>['_id'=>'$my','num'=>['$sum'=>'$my']]],
                ]);
            $document = current($cursor->toArray());
            if(!empty($document))
            {
                $num = $document->num;
            }
        }
        catch(\Exception $e)
        {
            \Yii::error($e->getMessage());
        }
        return $num;
    }

    /**
     * 购买数
     * @param $where
     * @param string $date
     * @return int
     */
    public static function getBuyNumbers($where,$date = ''){
        $where['total_price'] = ['$gt'=>0];
        $num = 0;
        try{
            $cursor = self::instance()->mongoCollection->aggregate(
                [
                    ['$match'=>$where],
                    ['$group'=>['_id'=>'$user_id','count'=>['$sum'=>1]]],
                    ['$project'=>['my'=>['$add'=>[1]]]],
                    ['$group'=>['_id'=>'$my','num'=>['$sum'=>'$my']]],
                ]);
            $document = current($cursor->toArray());
            if(!empty($document))
            {
                $num = $document->num;
            }
        }
        catch(\Exception $e)
        {
            \Yii::error($e->getMessage());
        }
        return $num;
    }

    /**
     * 订单数
     * @param $where
     * @param string $date
     * @return int
     */
    public static function getOrderNumber($where,$date = ''){
        $where['order_no'] = ['$ne'=>''];
        $num = 0;
        try{
            $cursor = self::instance()->mongoCollection->aggregate(
                [
                    ['$match'=>$where],
                    ['$group'=>['_id'=>'$order_no','count'=>['$sum'=>1]]],
                    ['$project'=>['my'=>['$add'=>[1]]]],
                    ['$group'=>['_id'=>'$my','num'=>['$sum'=>'$my']]],
                ]);
            $document = current($cursor->toArray());
            if(!empty($document))
            {
                $num = $document->num;
            }
        }
        catch(\Exception $e)
        {
            \Yii::error($e->getMessage());
        }
        return $num;
    }

    /**
     * 销售总额
     * @param $where
     * @param string $date
     * @return int
     */
    public static function getTotalSales($where,$date = '')
    {
        $num = 0;
        try{
            $cursor = self::instance()->mongoCollection->aggregate([
                    ['$match'=>$where],
                    ['$group'=>['_id'=>'1','num'=>['$sum'=>'$total_price']]]
                ]);
            $document = current($cursor->toArray());
            if(!empty($document))
            {
                $num = $document->num;
            }
        }
        catch(\Exception $e)
        {
            \Yii::error($e->getMessage());
        }
        return $num;
    }

    protected static function getConfig()
    {
        return [
            'dsn'=> \Yii::$app->params['mongo.group.visitor']['dsn'],
            'database'=> \Yii::$app->params['mongo.group.visitor']['database'],
            'tablePrefix'=> \Yii::$app->params['mongo.group.visitor']['collection'],
        ];
    }
}